/*-----------------------------------------------------
 * 权限子系统-系统文档模块
 * ---------------------------------------------------*/
jx.ready(function () {
    //api变量
    var api = {
        category: '/auth/dicdetails/queryTreeByDicCode/DicSystemDocCategory',
        grid: '/auth/doc/grid',
        create: '/auth/doc/create',
        edit: '/auth/doc/edit',
        delete: '/auth/doc/delete',
        details: '/auth/doc/details',
        status: '/auth/doc/status'
    }
    //定义变量
    var $tree = $('#tree'), $grid = $('#grid'), $gridPanel
    var gridInstance, treeInstance, lastSelectedId;
    var dialogWidth = $grid.data('dialogWidth'), dialogHeight = $grid.data('dialogHeight');
    var gridUrl = jx.apiUrl(api.grid), createUrl = jx.apiUrl(api.create),
        editUrl = jx.apiUrl(api.edit), deleteUrl = jx.apiUrl(api.delete),
        detailsUrl = jx.apiUrl(api.details), statusUrl = jx.apiUrl(api.status),
        categoryUrl = jx.apiUrl(api.category);

    //初始化树控件
    var initTree = function () {
        //树控件初始化参数
        treeInstance = $tree.jxtree({
            url: categoryUrl,
            onSelect: function (node) {
                if (lastSelectedId === node.id) {
                    return;
                }
                lastSelectedId = node.id;
                gridInstance.reloadGridData();
                //设置右边区域标题
                $(document.body).layout('panel', 'center').panel('setTitle', node.text);
            },
            onLoadSuccess: function () {
                var node;
                var roots = treeInstance.getRoots();
                if (roots.length > 0) {
                    node = roots[0];
                    $(this).jxtree('insert', {
                        before: node.target,
                        data: {
                            id: '-1',
                            text: '全部',
                            iconCls: 'icon-screen-desktop'
                        }
                    });
                    node = treeInstance.getRoots()[0];
                }
                if (node) {
                    treeInstance.expandTo(node.target);
                    treeInstance.expand(node.target);
                    treeInstance.select(node.target);
                    treeInstance.scrollTo(node.target);
                }
            }
        });
    };

    //初始化表格
    var initGrid = function () {
        gridInstance = $grid.jxgrid({
            url: gridUrl,
            onBeforeLoad: function (param) {
                var item = treeInstance.getSelected();
                if (!item) return false;

                param.categoryId = item.id;
                return true;
            },
            onDblClickRow: function (index, row) {
                detailsData(row);
            },
            onRowContextMenu: function (e, index, row) {
                jx.auth.gridContextMenuHandle(gridInstance, e, index, row)
            },
            onLoadSuccess: function () {
                jx.auth.showGridContextMenu(gridInstance, '#gridcmenu');
            }
        });
        $gridPanel = gridInstance.getPanel();
    };

    //初始化事件
    var initEvent = function () {
        $('#btn-create').click(function () {
            createData();
        });
        $('#btn-edit,#btn-cmedit').click(function () {
            editData();
        });
        $('#btn-delete,#btn-cmdelete').click(function () {
            deleteData();
        });

        // 表格事件
        $gridPanel.on('click', '.gridstatus', function () {
            var id = $(this).data('id');
            var val = $(this).data('val');
            setStatus(id, val);
        });
    };

    //新建数据
    var createData = function () {
        var selectedNode = treeInstance.getSelected();
        var params = {};
        if (selectedNode && selectedNode.id != '-1') {
            params = {categoryId: selectedNode.id};
        }
        jx.dialog({
            title: '新增文档',
            url: createUrl,
            params: params,
            width: dialogWidth,
            height: dialogHeight
        });
    };

    //编辑数据
    var editData = function () {
        jx.auth.editData(gridInstance, {
            title: '修改文档',
            url: editUrl,
            width: dialogWidth,
            height: dialogHeight
        });
    };

    //删除数据
    var deleteData = function () {
        jx.auth.deleteData(gridInstance, {
            url: deleteUrl
        });
    };

    //查看数据
    var detailsData = function (row) {
        jx.auth.detailsData(gridInstance, row, {
            title: false,
            url: detailsUrl,
            // maxmin: true,
            width: '100%',
            closeBtn: 0,
            shadeClose: true
        });
    };

    //设置数据状态
    var setStatus = function (id, val) {
        var status = val == '1' ? 0 : 1;
        jx.ajax({
            url: statusUrl,
            data: {id: id, status: status},
            maskMsg: '正在更新状态,请稍等...',
            success: function (result) {
                gridInstance.reloadGridData();
                jx.toastr.success('状态更新成功');
            }
        });
    };

    //定义表格列格式化函数
    jx.gf.docStatus = function (v, row) {
        var id = row.id;
        var cls = v == '1' ? 'success' : 'danger';
        var text = v == '1' ? '发布' : '草稿';
        return '<span class="gridstatus label label-' + cls + '" data-id="' + id + '" data-val="' + v + '">' + text + '</span>';
    };

    //定义表格列格式化函数
    jx.gf.docFormat = function (v, row) {
        var id = row.id;
        var cls = v == '1' ? 'success' : 'primary';
        var text = v == '1' ? 'HTML' : 'Md';
        return '<span class="label label-' + cls + '">' + text + '</span>';
    };

    //对外接口
    //重新加载表格数据
    window.reloadGridData = function () {
        gridInstance.reloadGridData();
    };

    //初始化
    initTree();
    initGrid();
    initEvent();
});
jx.complete(function () {
    jx.monitorLayoutPanel($(document.body), 'west', 'spring-sysdoc');
});